﻿@model List<SmallJob.Model.Order>
@{
    var tablename = "TableOrder";
    var colsname = new[] { "Buyer", "Seller", "Title", "Price", "Status", "Payment", "Type" };
    var curaction = "Index";
    var curcontroller = "Order";
    var configaction = new { details = "Details", del = "Lock", create = "Create", undel = "UnLock" };
    var configtext = new { details = "Details", del = "Lock", create = "Create", undel = "UnLock" };
    var isfilter = !(ViewData["filter"] == null);
    var urlpaging = "/" + curcontroller
        + "/" + curaction
        + "?filter=" + ViewData["filter"]
        + "&filtertype=" + ViewData["filtertype"]
        + "&filterstatus=" + ViewData["filterstatus"]
        + "&filterpayment=" + ViewData["filterpayment"];
    var intvalue = -1;
    
}
<br />
<input type="text" name="search" value="@(isfilter ? ViewData["filter"] : "")" id="inputsearch" onchange="triggersearch()"/>
Search Here, Type and Enter
<br />
<select id="select1" onchange="triggersearch()">
    @{      intvalue = -1;
            string[] name1s = Enum.GetNames(typeof(SmallShop.Services.Code.OrderStatusV2));
            int[] value1s = (int[])Enum.GetValues(typeof(SmallShop.Services.Code.OrderStatusV2));
            for (int i = 0; i < value1s.Length; i++)
            {
                Int32.TryParse((String)ViewData["filterstatus"] ?? "-1", out intvalue);
        <option value="@value1s[i]" @(intvalue == value1s[i] ? "selected" : "")>@name1s[i]</option>
            }
        <option value="-1" @(intvalue == -1 ? "selected" : "")>All</option>
        intvalue = -1;
    }
</select>
<select id="select2" onchange="triggersearch()">

    @{
        intvalue = -1;
        string[] name2s = Enum.GetNames(typeof(SmallJob.Model.EPaymentStatus));
        int[] value2s = (int[])Enum.GetValues(typeof(SmallJob.Model.EPaymentStatus));
        for (int i = 0; i < value2s.Length; i++)
        {
            Int32.TryParse((String)ViewData["filterpayment"] ?? "-1", out intvalue);
        <option value="@value2s[i]" @(intvalue == value2s[i] ? "selected" : "")>@name2s[i]</option>
        }
        <option value="-1" @(intvalue == -1 ? "selected" : "")>All</option>
        intvalue = -1;
    }
</select>
<select id="select3" onchange="triggersearch()">
    @{
        intvalue = -1;
        string[] name3s = Enum.GetNames(typeof(SmallJob.Services.Implements.OrderType));
        int[] value3s = (int[])Enum.GetValues(typeof(SmallJob.Services.Implements.OrderType));
        for (int i = 0; i < value3s.Length; i++)
        {
            Int32.TryParse((String)ViewData["filtertype"] ?? "-1", out intvalue);
        <option value="@value3s[i]" @(intvalue == value3s[i] ? "selected" : "")>@name3s[i]</option>
        }
        <option value="-1" @(intvalue == -1 ? "selected" : "")>All</option>
        intvalue = -1;
    }
</select>
<div class="div-parent">

    <table class="table table-hover sort">
        <thead>
            <tr>
                @for (int i = 0; i < colsname.Count(); i++)
                {
                    <th>@colsname[i]
                        <span style="float: right;"><i class="icon-sort"></i></span>
                    </th>
                }
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        <a href="@Url.Action("Details", "Order", new { id=item.ID,userid=item.BuyerID})" target="_blank">@Html.DisplayFor(modelItem => item.Buyer.ProperName)</a>
                    </td>
                    <td>
                        <a href="@Url.Action("Details", "Order", new { id=item.ID,userid=item.SellerID})" target="_blank">@Html.DisplayFor(modelItem => item.Seller.ProperName)  </a>
                    </td>
                    <td>
                        @(item.Provide == null ? item.Request.Title : item.Provide.Title)
                    </td>
                    <td>
                        @item.Price
                    </td>
                    <td>
                        @(((SmallShop.Services.Code.OrderStatusV2)item.Status).ToString())
                    </td>
                    <td>
                        @(((SmallJob.Model.EPaymentStatus)item.PaymentStatus).ToString())
                    </td>
                    <td>
                        @(item.Type == 1 ? "Dịch vụ" : "Nhu cầu")
                    </td>
                </tr>
            }
        </tbody>

    </table>

    <div class="pagination">
        <ul>
            @{
                var total = Convert.ToInt32(ViewData["total"]);
                var curpage = Convert.ToInt32(ViewData["curpage"]);
                if (curpage > 1)
                {
                <li><a href="@(urlpaging + "&page=" + (curpage - 1))">Prev</a></li>
                }
                for (int i = 1; i <= total; i++)
                {
                    if (i != curpage)
                    {
                <li><a href="@(urlpaging + "&page=" + i)">@i</a></li>
                    }
                    else
                    {
                <li><a href="@(urlpaging + "&page=" + i)"><strong>@i</strong></a></li>
                    }

                }
                if (curpage < total)
                {
                <li><a href="@(urlpaging + "&page=" + (curpage + 1))">Next</a></li>
                }
            }
        </ul>
    </div>
</div>

<script src="@Url.Content("~/Content/js/jquery.tablesorter.min.js")"></script>
<script>
    //table sort
    $(function () {
        $("table.sort").tablesorter();
    });
    $("table").bind("sortStart", function () {
    }).bind("sortEnd", function () {
        $('th.header').each(function () {
            if ($(this).hasClass('headerSortDown')) {
                $(this).find('.icon-sort').addClass('icon-hand-up');
                $(this).find('.icon-sort').removeClass('icon-hand-down');

            }
            if ($(this).hasClass('headerSortUp')) {
                $(this).find('.icon-sort').addClass('icon-hand-down');
                $(this).find('.icon-sort').removeClass('icon-hand-up');
            }
            if (!$(this).hasClass('headerSortDown') && !$(this).hasClass('headerSortUp')) {
                $(this).find('.icon-sort').removeClass('icon-hand-down');
                $(this).find('.icon-sort').removeClass('icon-hand-up');
            }
        });
    });
    //del and undel ajax
    function del(source, id) {
        $.ajax({
            type: "POST",
            url: '@Url.Action(configaction.del, curcontroller)',
            data: { id: id },
            success: function (data) {
                if (data == true) {
                    $(source).text('@configtext.undel');
                    $(source).attr('onClick', 'undel(this,' + id + ')');
                } else {
                }
            }
        });
    }
    function undel(source, id) {
        $.ajax({
            type: "POST",
            url: '@Url.Action(@configaction.undel, curcontroller)',
            data: { id: id },
            success: function (data) {
                if (data == true) {
                    $(source).text('@configtext.del');
                    $(source).attr('onClick', 'del(this,' + id + ')');
                } else {
                }
            }
        });
    }
    function triggersearch() {
        var filter = $('#inputsearch').val();
        var filterstatus = $('#select1').val();
        var filterpayment = $('#select2').val();
        var filtertype = $('#select3').val();
        $.ajax({
            type: "POST",
            url: '@Url.Action(@tablename, curcontroller)',
            data: { target: 1, page: 1, pagesize: 1, filter: filter, filterstatus: filterstatus, filterpayment: filterpayment, filtertype: filtertype },
            success: function (data) {
                if (data != null) {
                    var getdata = $(data).filter('.div-parent');
                    $('.div-parent').html(getdata.html());
                } else {
                }
            }
        });
    }
</script>
